﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.MBSM.BLL;
using HZ.MBSM.Model;
using System.Text;
public partial class Agency_JM_rand_Ca : System.Web.UI.Page
{
    BCACOMLib.SecurityEngineClass bca = new BCACOMLib.SecurityEngineClass();
    public string strRan = "";
    public string strSignData = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAction.Attributes.Add("OnClick", "javascript:return InputCheck()");
        if (strRan == null || strRan == "" || Session["Random"] == null || Session["Random"].ToString() == "")
        {
            //获取随机数
            strRan = bca.generateRandom(24);
            //签名随机数
            strSignData = bca.signedDataByP7(strRan);
            Session["Random"] = strRan;
        }
        if (!IsPostBack)
        {

        }
    }
    /// <summary>
    /// 分段随机数解密
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAction_Click(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        strRan = Session["Random"].ToString();//签名随机数

        //容器名,签名需要
        string strContainerName = hddContainerName.Value;
        Session["ContainerName"] = strContainerName;

        //用户证书,验证签名需要
        string strUserCert = Request.Form.Get("UserCert");
        Session["UserCert"] = strUserCert;

        //加密证书
        string strUserEncCert = this.hddEncCert.Value;
        Session["UserEncCert"] = strUserEncCert;

        //用户签名值
        string strUserSignData = Request.Form.Get("UserSignedData");

        int rv = bca.validateCertEx(strUserCert); //验证用户证书
        //验证证书
        if (rv != 0)
        {
            return;
        }

        rv = bca.verifySignedDataByP7(strUserSignData);//验证签名
        //验证签名
        if (rv != 0)
        {
            return;
        }

        //获取唯一标示(数据库验证)
        string strUID = bca.getExtCertInfoByOID(strUserCert, "1.2.86.11.7.1.8");


        if (strUID == "")
        {
            return;
        }
        //检验插入的key是否在解密设置表中存在
        //若存在则对解密参数表中的数据进行解密
        EDCertCA edc = new EDCertCA();
        if (edc.IsJMKey(strUID, SysFun.IsSQL(Request.QueryString["projectid"]), 0, 1))
        {
            // 验证key是否已经使用
            if (!edc.IsKeyUsered(strUID, SysFun.IsSQL(Request.QueryString["projectid"]), 0, 1))
            {

                try
                {

                    List<V_JM_RenByKey> lst = edc.GetV_JM_RenByKeyInfo(strUID, SysFun.IsSQL(Request.QueryString["projectid"]), _Account.AccountId.ToString(), 1);
                    int lstCount = lst.Count;
                    if (lstCount > 0)
                    {
                        for (int _index = 0; _index < lstCount; _index++)
                        {
                            lst[_index].JM_DeRen_UKEY = bca.envelopedData(lst[_index].JM_EnRen_UKEY, strContainerName, 0);
                        }
                        //更改数据库信息

                        edc.JM_DeRen_UKEY_Update(lst, edc.GET_JM_doorSetid(strUID, SysFun.IsSQL(Request.QueryString["projectid"])), _Account.AccountId.ToString(), 0, 1);
                    }
                    //解密key  
                    Response.Write("<script>alert('操作成功请继续');</script>");

                }
                catch (Exception ex)
                {

                    //  AjaxMessageBox.AjaxMessageAlert(Message_ID.System_004, WriteErrorLog(ex), this.UpdatePanel1);
                }
            }
            else
            {
                Response.Write("<script>alert('该key已经解密完成！');</script>");

            }
        }
        else
        {
            Response.Write("<script>alert('插入的key不是解密设置中的key！');</script>");

        }
    }
    /// <summary>
    /// 分段随机数解密成功后进入下一步的产品报价解密
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnNext_Click(object sender, EventArgs e)
    {
        //还原所需最少份数

        int intCount = 2;

        //需添加分段随机数是否达到了需要的还原所需最少份数的友情提醒
        EDCertCA edc = new EDCertCA();
        if (edc.GetJMKEY_Count(SysFun.IsSQL(Request.QueryString["projectid"])) >= intCount)
        {
            try
            {
                //解密企业的随机数
                List<JM_RenByKey> lst = edc.GetJM_RenByKeyInfo("1");
                int lstCount = lst.Count;
                if (lstCount > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (JM_RenByKey jm_ren in lst)//遍历解密数据
                    {
                        sb.Remove(0, sb.Length);
                        for (int i = 0; i <= intCount; i++)//链接企业解密数据
                        {
                            if (jm_ren.JM_DeRen_UKEY != null && jm_ren.JM_DeRen_UKEY != "")//判断是否已经解密
                            {
                                sb.Append("&&&");
                                sb.Append(lst[i].JM_DeRen_UKEY);
                            }
                        }
                        if (sb.Length >= 3)//是否有链接起来
                        {
                            sb.Remove(0, 3);
                            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                            //参数：BSTR sIndata  以“&&&”连接的分组后的密钥。
                            //返回值： BSTR 还原的原始密钥
                            string strCompnayRan = bca.SecertRecovery(sb.ToString());
                            edc.CompanyJMupdate(SysFun.IsSQL(Request.QueryString["projectid"]), jm_ren.JM_accountid_qy, _Account.AccountId.ToString(), strCompnayRan);
                        }

                    }


                }
                Response.Redirect("PriceOfGoodsToDecryptTHreeWithCA.aspx?projectid=" + SysFun.IsSQL(Request.QueryString["projectid"]) + "&ID2=" + SysFun.IsSQL(Request.QueryString["ID2"]));
            }
            catch (Exception ex)
            {

            }

        }
        else
        {
            // AjaxMessageBox.AjaxMessageAlert("未达到还原所需最少份数，请继续！", this.UpdatePanel1);
        }
    }
}

